package com.wuwei.baseproject;

import android.app.Activity;
import android.content.Context;
import android.os.Process;
import android.support.multidex.MultiDex;
import android.support.multidex.MultiDexApplication;
import com.union.utils.ToastUtils;

import java.util.Stack;

public class UnionApplication extends MultiDexApplication {
    public static UnionApplication instance;
    private static Stack<Activity> atyStack = new Stack<Activity>();
    private boolean isDebug=true;

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(base);
    }

    @Override
    public void onCreate() {
        super.onCreate();
        ToastUtils.init(this);
    }

    public static void push(Activity aty) {
        atyStack.push(aty);
    }

    @Override
    public void onTerminate() {
        super.onTerminate();
        while (!atyStack.empty()) {
            atyStack.pop().finish();
        }
        Process.killProcess(Process.myPid());
    }



    public static void closeSeries(Class<? extends  Activity> cls) {
        if (atyStack.isEmpty()) {
            return;
        }
        for (int pos = findPos(cls); pos < atyStack.size() - 1; ) {
            atyStack.pop().finish();
        }
    }

    private static int findPos(Class<? extends  Activity> cls) {
        for (int pos = 0; pos < atyStack.size(); pos++) {
            if (atyStack.get(pos).getClass() == cls) {
                return pos;
            }
        }
        throw new IllegalStateException();
    }

    public static void pop(Activity aty) {
        atyStack.remove(aty);
    }
}
